package service;

import cn.hutool.core.util.ObjectUtil;
import dao.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pojo.User;
import until.SqlSessionFactoryUtils;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 注册的servlet
 */
public class UserRegisterService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    public  final AtomicLong uniqueId = new AtomicLong(System.currentTimeMillis());
    public  long generateUniqueId() {
        return uniqueId.incrementAndGet();
    }
    public int searchUser(String username,String password,String phone){
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.selectRegister(username);

        if (ObjectUtil.isEmpty(user)){
            long id = generateUniqueId();
            int i = mapper.insertUser(id,username,password,phone);
            sqlSession.commit();
            sqlSession.close();
            System.out.println(i);
            if (i==1){
                return 1;
            }
        }
        return 0;
    }
    public User searchUser2(String username,String password){
        SqlSession sqlSession = factory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.select(username, password);
    }
}
